<?php

namespace app\admin\model;

use think\Db;
use think\Model;

class Hospital extends Model
{
    protected $table = 'petct_hospital';
    public $notes = array(
        'firsttitle' => array(0 => '无推荐', 1 => '一级推荐', 2 => '二级推荐', 3 => '三级推荐', 4 => '四级推荐', 5 => '五级推荐', 6 => '六级推荐', 7 => '七级推荐', 8 => '八级推荐', 9 => '九级推荐'),
        'isgood' => array(0 => '无头条', 1 => '一级头条', 2 => '二级头条', 3 => '三级头条', 4 => '四级头条', 5 => '五级头条', 6 => '六级头条', 7 => '七级头条', 8 => '八级头条', 9 => '九级头条'),
        'istop' => array(0 => '无置顶', 1 => '一级置顶', 2 => '二级置顶', 3 => '三级置顶', 4 => '四级置顶', 5 => '五级置顶', 6 => '六级置顶', 7 => '七级置顶', 8 => '八级置顶', 9 => '九级置顶'),
    );

    /**
     * 获取医院信息
     * @param $limit
     * @param $where
     * @return array
     */
    public function getHos($limit, $where)
    {
        try {

            $res = $this->where($where)
                ->order('id', 'desc')->paginate($limit);

        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }

    /**
     * 增加医院
     * @param $params
     * @return array
     */
    public function addHos($params)
    {
        try {

            $where = [
                'hos_name' => $params['hos_name']
            ];
            $flag = $this->where($where)->count();
            if ($flag) {
                return modelReMsg(-2, '', '医院已经存在');
            }

            $params['add_time'] = date('Y-m-d H:i:s');
            if (empty($params['hos_bname'])) {
                $params['hos_bname'] = $params['hos_name'];
            }

            $this->insert($params);
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '医院添加成功');
    }

    /**
     * 根据id获取医院信息
     * @param $id
     * @return array
     */
    public function getHosById($id)
    {
        try {

            $info = $this->where('id', $id)->findOrEmpty()->toArray();
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $info, 'ok');
    }

    /**
     * 编辑医院
     * @param $params
     * @return array
     */
    public function editHos($params)
    {
        try {

            $has = $this->where('hos_name', $params['hos_name'])
                ->where('id', '<>', $params['id'])
                ->findOrEmpty()->toArray();

            if (!empty($has)) {
                return modelReMsg(-2, '', '医院已经存在');
            }
            $params['update_time'] = date('Y-m-d H:i:s');

            $this->save($params, ['id' => $params['id']]);
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '编辑医院成功');
    }

    /**
     * 删除医院
     * @param $id
     * @return array
     */
    public function delHos($id)
    {
        try {

            $this->where('id', $id)->delete();
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '删除成功');
    }
}